package com.example.hoasbackend.web.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.hoasbackend.common.JsonResponse;
import com.example.hoasbackend.service.CommentService;
import com.example.hoasbackend.model.domain.Comment;

import java.util.List;


/**
 *
 *  前端控制器
 *
 *
 * @author lhx
 * @since 2022-03-05
 * @version v1.0
 */
@Controller
@RequestMapping("/api/comment")
public class CommentController {

    private final Logger logger = LoggerFactory.getLogger( CommentController.class );

    @Autowired
    private CommentService commentService;

    /**
    * 描述：根据Id 查询
    *
    */
    @RequestMapping(method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse getById(@RequestParam("id") Long id)throws Exception {
        Comment  comment =  commentService.getById(id);
        return JsonResponse.success(comment);
    }
    /**
     * 描述：根据酒店Id 查询
     *
     */
    @RequestMapping(value = "/get_hotel", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse getByHotelId(@RequestParam("id") Long id)throws Exception {
        List<Comment> comments =  commentService.getByHotelId(id);
        if(comments!=null&&comments.size()>0)
        return JsonResponse.success(comments);
        return JsonResponse.failure("InvalidComments.NotFound").setCode(404);

    }
    /**
    * 描述：根据Id删除
    *
    */
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    @ResponseBody
    public JsonResponse deleteById(@RequestParam("id") Long id) throws Exception {
        commentService.removeById(id);
        return JsonResponse.success(null);
    }


    /**
    * 描述：根据Id 更新
    *
    */
    @RequestMapping(method = RequestMethod.PUT)
    @ResponseBody
    public JsonResponse updateComment(@RequestParam("id") Long  id,Comment  comment) throws Exception {
        comment.setId(id);
        commentService.updateById(comment);
        return JsonResponse.success(null);
    }


    /**
    * 描述:创建Comment
    *
    */
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public JsonResponse create(Comment  comment) throws Exception {
        commentService.save(comment);
        return JsonResponse.success(null);
    }
}

